package com.shadow.demo.generator;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;

public class TestMain {

    public static void main(String[] args) {
//        getPath();
//        getPath2();
//        getPath3();
        getPath4();
    }

    /**
     * Paths.get("") 来获取当前项目的相对路径
     * 并将其转换为绝对路径
     * >>> D:\workspace\projects-demo\demo
     */
    public static void getPath() {
        String currentDirectory = Paths.get("").toAbsolutePath().toString();
        System.out.println("1. Current Directory: " + currentDirectory);
    }

    /**
     * System.getProperty("user.dir") 获取的是程序启动时的工作目录
     * 通常是应用程序启动时所在的路径
     * >>> D:\workspace\projects-demo\demo
     */
    public static void getPath2() {
        String currentDirectory = System.getProperty("user.dir");
        System.out.println("2. Current Directory: " + currentDirectory);
    }

    public static void getPath3() {
        // 获取当前项目路径
        String projectDirectory = System.getProperty("user.dir");
        // 指定 gen 目录的路径
        String genDirectoryPath = projectDirectory + File.separator + "a-gen";
        // 创建目录
        File genDirectory = new File(genDirectoryPath);
        if (!genDirectory.exists()) {
            // 目录不存在，创建目录
            boolean created = genDirectory.mkdirs();
            if (created) {
                System.out.println("目录 " + genDirectoryPath + " 创建成功");
            } else {
                System.out.println("目录 " + genDirectoryPath + " 创建失败");
            }
        } else {
            System.out.println("目录 " + genDirectoryPath + " 已存在");
        }

        // 创建一个示例文件
        File generatedFile = new File(genDirectoryPath + File.separator + "example.txt");
        try {
            if (generatedFile.createNewFile()) {
                System.out.println("文件 " + generatedFile.getName() + " 已生成");
            } else {
                System.out.println("文件 " + generatedFile.getName() + " 已存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getPath4() {
        URL testMain = TestMain.class.getClassLoader().getResource("TestMain");
        System.out.println(testMain);
        String name = TestMain.class.getPackage().getName();
        System.out.println("TestMain.class.getPackage().getName() : " + name);
//        TestMain.class.getClassLoader().getResourceAsStream()

    }
}
